<?php

/**
 * 修改用户密码
 *
 * @author suning <snsnsky@gmail.com>
 */

use lib\util\Securimage;
use Passport\User as PUser;
use Encrypt\Rsa;

define("NO_CHECK", true);

require('common.inc.php');

class update_pwd extends \lib\core\AdminAction
{
    public function doDefault()
    {
        if ($usn = $_SESSION['admin_username'] == '')
        {
            session_destroy();
            $this->app->redirect('index.php');
        }
        else
        {
            $usn = $_SESSION['admin_username'];

            $smarty = $this->app->page();
            $smarty->value('usn', $usn);
            $smarty->output('update_pwd.tpl');
        }
    }

    public function doUpdatePwd()
    {
        $username = $_SESSION['admin_username'];
        $pwd = trim($_POST['pwd']);
        $pwds = trim($_POST['pwds']);
        $pwsold = Rsa::private_decode($_POST['pwsold']);
        $code = trim($_POST['code']);
        $length = strlen($pwds);
        $lengths = strlen($pwd);

        $img = new Securimage();
        if ($img->check($code) === false)
        {
            $this->output(['title'=>'验证码错误', 'type'=>'error']);
        }

        if ($length < 7 || $lengths < 7)
        {
            $this->output(['title'=>'密码长度不符合标准', 'type'=>'error']);
        }

        if ($pwd != $pwds || $pwds == '' || $pwd =='')
        {
            $this->output(['title'=>'密码输入不一致或为空', 'type'=>'error']);
        }

        $user = \Passport\Login::doLogin($username, $pwsold, false);
        if (!isset($user['UserId']))
        {
            $this->output(['title'=>'旧密码错误','type'=>'error']);
        }

        //更新用户信息
        $re = self::checkPwdStrong($pwd);

        if ($re < 3)
        {
            self::output(['title' => '修改失败', 'text' => '密码不符合安全标准,请详细阅读提示再做修改', 'type' => 'error']);
        }

        $res = PUser::updatePassword($user['UserId'], '', $pwds, 1);
        if ($res == true)
        {
            unset( $_SESSION['admin_username']);
            $admin = \Admin\AdminAuth::getAdminUserById($user['UserId']);
            $auth = unserialize($admin['SpecialGroups']);
            $group_auth = unserialize($admin['GroupData']['AdminAuth']);
            $auth = array_merge((array)$auth, (array)$group_auth);
            $_SESSION['admin_auth_list'] = array_combine($auth, $auth);
            $_SESSION['admin_info'] = $admin;

            $this->adminLog('修改密码', $admin['AdminUserId'], 'updata', '');
            $this->adminLog('登录成功', $admin['AdminUserId'], 'login', '');
            self::output(['title' => '修改成功', 'text' => '点击确定自动跳转到后台', 'type' => 'success']);
        }
    }

    /**
     * 检查密码复杂度
     *
     * @param string $pwd 密码
     * @return int           复杂度 0-4, 越高复杂度越高
     */
    public static function checkPwdStrong($pwd)
    {
        if ((strlen($pwd) < 8 && preg_match('/^\d+$/', $pwd)) || empty($pwd)) {
            return 0;
        }

        $number = preg_match('/\d/', $pwd) ? 1 : 0;
        $upper = preg_match('/[A-Z]/', $pwd) ? 1 : 0;
        $lower = preg_match('/[a-z]/', $pwd) ? 1 : 0;
        $symbol = preg_match('/^[a-zA-Z0-9]+$/', $pwd) ? 0 : 1;

        return ($number + $upper + $lower + $symbol);
    }
}

$app->run();
